package util.PluginTools;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import com.supermap.desktop.WorkflowView.ui.ProcessTree;
import operation.ModelOperation;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.io.*;


/**
 * Created by hanyz on 2017/7/4.
 * 将process树按结构生成到文件，用notepad打开全选复制到excel，行数即功能数目
 */


public class ProcessTreeToFile {
	BufferedWriter bw = null;
	ProcessTree tree = null;
	int level = 0;

	//调用测试方法得到文件，这是一个用例
	public void getTreeNodes() {
		try {
			ModelOperation.startModeling();
			tree = (ProcessTree) new BasicFinder().find(new ClassMatcher(ProcessTree.class));
			File file = new File("F:\\tmp\\process.txt");
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
					"UTF-8"));
			DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
			System.out.println(root);
			getChild(root);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void getChild(DefaultMutableTreeNode treeNode) {
		if (treeNode == null) {
			return;
		}
		String strLabel = null;
		String name;
		JComponent nodeCmp = (JComponent) tree.getCellRenderer()
				.getTreeCellRendererComponent(tree,
						treeNode, false, false, false, 0,
						false);

		try {
			JLabel label = (JLabel) new BasicFinder().find(
					nodeCmp, new ClassMatcher(JLabel.class));
			if (label != null) {
				strLabel = label.getText();
			}
		} catch (ComponentNotFoundException
				| MultipleComponentsFoundException e) {
			e.printStackTrace();
		}

		boolean firstChild = false;
		if (treeNode.getParent() != null) {
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeNode.getParent();
			if (parent.getChildBefore(treeNode) == null) {
				firstChild = true;
			}
		}
		boolean lastChild = false;
		if (treeNode.getParent() != null && ((DefaultMutableTreeNode) treeNode.getParent()).getChildAfter(treeNode) == null) {
			lastChild = true;
		}
		boolean isGroup = false;
		if (treeNode.getChildCount() > 0 && !strLabel.equals("ProcessManager")) {
			isGroup = true;
		}
		if (firstChild) {
			if (level == 0) {
				name = strLabel;
			} else {
				name = "\t" + strLabel;
			}
		} else {
			if (isGroup) {
				name = getTabs() + strLabel;
			} else {
				name = getTabs() + strLabel;
			}
		}
		if (!isGroup) {
			name += "\n";
		} else {
			level += 1;
		}

		try {
			if (!strLabel.equals("ProcessManager")) {
				bw.write(name);
				bw.flush();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < treeNode.getChildCount(); i++) {
			getChild((DefaultMutableTreeNode) treeNode.getChildAt(i));
		}

		if (lastChild) {
			level -= 1;
		}
	}

	private String getTabs() {
		String tabs = "";
		for (int i = 0; i < level; i++) {
			tabs += "\t";
		}
		return tabs;
	}
}

